﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SpaceInvaders
{
    public static class GerenciadorDeTiro
    {
        public static Texture2D textureEnemy;
        public static Texture2D texturePlayer;
        public static Tiro[] playerShots { get; set; }
        public static Tiro[] enemyShots { get; set; }

        public static int playerTimer = 0;

        public static void Add(float x, float y, int w, int h, bool isEnemy)
        {
            if (isEnemy == false)
            {
                playerShots = Tiro.AddToList(playerShots, new Tiro(new Vector2(x, y), new Vector2(w, h), Color.White, texturePlayer));
            }
            else
            {
                enemyShots = Tiro.AddToList(enemyShots, new Tiro(new Vector2(x, y + h*2), new Vector2(w, h), Color.White, textureEnemy));
            }
        }

        public static void Update()
        {
            playerTimer++;

            if (playerShots != null)
            {
                for (int n = 0; n < playerShots.Length; n++)
                {
                    if (playerShots == null || GerenciadorDeInimigo.enemies == null)
                        return;

                    for (int e = 0; e < GerenciadorDeInimigo.enemies.Length; e++)
                    {
                        if (playerShots == null || GerenciadorDeInimigo.enemies == null)
                            return;
                        if(playerShots[n].CollisionRect().Intersects(GerenciadorDeInimigo.enemies[e].CollisionRect()))
                        {
                            Ponto.AumentaPontuacao(50);
                            playerShots = Tiro.RemoveAtIndex(playerShots, n);
                            GerenciadorDeInimigo.enemies = Inimigo.RemoveAtIndex(GerenciadorDeInimigo.enemies, e);
                        }
                        
                        if (playerShots == null || GerenciadorDeInimigo.enemies == null)
                            return;

                        if (n >= playerShots.Length)
                            break;
                    }
                }

                for (int n = 0; n < playerShots.Length; n++)
                {
                    if (playerShots[n].position.Y <= 0)
                        playerShots = Tiro.RemoveAtIndex(playerShots, n);
                    else
                        playerShots[n].Move(-5f);

                    if (playerShots == null)
                        return;
                }
            }

            if (enemyShots != null)
            {
                for (int n = 0; n < enemyShots.Length; n++)
                {
                    if (enemyShots[n].CollisionRect().Intersects(Jogador.collisionRect))
                    {
                        GerenciadorDeCena.perdeuVida = true;
                    }
                    else if (enemyShots[n].position.Y > 600)
                        enemyShots = Tiro.RemoveAtIndex(enemyShots, n);
                    else
                        enemyShots[n].Move(5f);

                    if (enemyShots == null)
                        return;
                }
            }
        }

        public static void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
        {
            if (playerShots != null)
            {
                foreach (Tiro tiro in playerShots)
                {
                    tiro.Draw(spriteBatch);
                }
            }

            if (enemyShots != null)
            {
                foreach (Tiro tiro in enemyShots)
                {
                    tiro.Draw(spriteBatch);
                }
            }
        }
    }
}
